﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using QLKS.Entity;

namespace QLKS.DAL
{
    public class DMPhongDAO
    {
        private SqlDataProvider dataProvider;

        public DMPhongDAO()
        {
            dataProvider = new SqlDataProvider();
        }

        public List<DMPhong> GetByAll(DMPhong obj)
        {
            List<DMPhong> lstData = new List<DMPhong>(); ;
            try
            {
                DataTable dt = dataProvider.ExecuteDataTable("sp_DMPhong_GetByAll"
                    , new string[] { "MaPhong", "TenPhong", "LoaiPhong", "GhiChu", "TinhTrang" }
                    , new object[] { obj.MaPhong, obj.TenPhong, obj.LoaiPhongID, obj.GhiChu, obj.TinhTrang }
                );
                DMPhong entity;
                foreach(DataRow myrow in dt.Rows)
                {
                    entity = new DMPhong();
                    entity.MaPhong = Convert.ToInt32(myrow["MaPhong"]);
                    entity.TenPhong = myrow["TenPhong"] is DBNull ? string.Empty : myrow["TenPhong"].ToString();
                    entity.LoaiPhongID = Convert.ToInt32(myrow["LoaiPhong"]);
                    entity.GhiChu = myrow["GhiChu"] is DBNull ? string.Empty : myrow["GhiChu"].ToString();
                    entity.TinhTrang = Convert.ToBoolean(myrow["TinhTrang"]);
                    try
                    {
                        if (entity.LoaiPhongID != null && entity.LoaiPhongID != 0)
                        {
                            entity.LoaiPhong = ((new DMLoaiPhongDAO()).GetByAll(new DMLoaiPhong(entity.LoaiPhongID))).FirstOrDefault();
                        }
                        else
                        {
                            entity.LoaiPhong = null;
                        }
                    }
                    catch
                    {
                        entity.LoaiPhong = null;
                    }

                    lstData.Add(entity);
                }
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lstData;
        }

        public bool Insert(DMPhong obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_DMPhong_Insert"
                    , new string[] { "MaPhong", "TenPhong", "LoaiPhong", "GhiChu", "TinhTrang" }
                    , new object[] { obj.MaPhong, obj.TenPhong, obj.LoaiPhongID, obj.GhiChu, obj.TinhTrang }
                );
            return i == 1 ? true : false;
        }

        public bool Update(DMPhong obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_DMPhong_Update"
                    , new string[] { "MaPhong", "TenPhong", "LoaiPhong", "GhiChu", "TinhTrang" }
                    , new object[] { obj.MaPhong, obj.TenPhong, obj.LoaiPhongID, obj.GhiChu, obj.TinhTrang }
                );
            return i == 1 ? true : false;
        }

        public bool Delete(DMPhong obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_DMPhong_Delete"
                    , new string[] { "MaPhong", "TenPhong", "LoaiPhong", "GhiChu", "TinhTrang" }
                    , new object[] { obj.MaPhong, obj.TenPhong, obj.LoaiPhongID, obj.GhiChu, obj.TinhTrang }
                );
            return i == 1 ? true : false;
        }
    }
}
